<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Action;

use Illuminate\Http\Request;

use App\Http\Requests;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class ActionController extends CommonController
{
    //首页
    public function index()
    {
        $data = Action::all();
        return view("admin.action.index",compact("data"));
    }
    //添加页面
    public function create()
    {
        return view("admin.action.addAction");
    }
    //添加动作
    public function store()
    {
        $input = Input::except("_token");
            $rules =[
                'ac_name'=>"required",
                'starttime'=>"required",
                'endtime'=>"required",
                'ac_fanwei'=>"required",
                'ac_content'=>"required"
            ];
            $message =[
                'ac_name.required'=>"请填写活动名称！",
                'starttime.required'=>"请填写活动开始时间！",
                'endtime.required'=>"请填写活动结束时间！",
                'ac_fanwei.required'=>"请填写活动范围！",
                'ac_content.required'=>"请填写活动内容！",
            ];
            $validator = Validator::make($input,$rules,$message);

            if($validator->passes()){
                $input['starttime'] = strtotime($input['starttime']);
                $input['endtime'] = strtotime($input['endtime']);
                $re = Action::create($input);
                if($re){
                   return redirect("admin/action");
                }else{
                    return back()->with("errors","添加失败，请重试！");
                }
            }else{
                //dd($validator->errors()->all());
                return back()->withErrors($validator);
            }



    }
    //编辑页面
    public function edit($aid)
    {
        $data = action::find($aid);
        return view("admin.action.editAction",compact("data"));
    }
    //编辑动作
    public function update($aid)
    {
        $input  = Input::except("_token","_method");
        //验证规则
        $rules =[
            'ac_name'=>"required",
            'starttime'=>"required",
            'endtime'=>"required",
            'ac_fanwei'=>"required",
            'ac_content'=>"required"
        ];
        //错误信息
        $message =[
            'ac_name.required'=>"请填写活动名称！",
            'starttime.required'=>"请填写活动开始时间！",
            'endtime.required'=>"请填写活动结束时间！",
            'ac_fanwei.required'=>"请填写活动范围！",
            'ac_content.required'=>"请填写活动内容！",
        ];
        //验证
        $validator = Validator::make($input,$rules,$message);
        if($validator->passes()){//通过
            $input['starttime'] = strtotime($input['starttime']);//将时间转为时间戳
            $input['endtime'] = strtotime($input['endtime']);
            $re = Action::where("aid",$aid)->update($input);
            if($re){
                return redirect("admin/action");
            }else{
                return back()->with("errors","编辑失败，请重试！");
            }
        }else{//未通过
            //dd($validator->errors()->all());
            return back()->withErrors($validator);
        }
    }
    //删除
    public function destroy($aid)
    {
        $re = Action::where("aid",$aid)->delete();
        if($re){
            $data = [
                "status" =>0,
                "message" =>"删除成功",
            ];
        }else{
            $data = [
                "status" =>1,
                "message" =>"删除失败",
            ];
        }
        return $data;
    }

    
    public function show()
    {

    }
}
